#coding=gbk
from weather import weather
from google.appengine.ext import webapp
from google.appengine.api import mail
from traceback import format_exc
from google.appengine.ext.webapp.util import login_required
from google.appengine.ext import db
from traceback import format_exc
import datetime, time
import database, igaelib
from igaelib import logging


class CronWeather(webapp.RequestHandler):
    def get(self):
        try:
            today = time.strftime('%Y%m%d')
            citycode = igaelib.GetGaeCfg('weather', 'city')
            logging.debug('city=%s, today=%s' %(citycode, today))
            mail.send_mail(sender='asdseal@gmail.com', to='18221655453@139.com', subject='%sweather' % today, 
                    body=weather(citycode))
        except:
            logging.error(format_exc())

class CronClock(webapp.RequestHandler):
    def get(self):
        pass
        

class CronClrHis(webapp.RequestHandler):
    @login_required
    def get(self):
        sqlstmt = 'select * from ClearRule'
        clearrule = db.GqlQuery(sqlstmt)
        for rule in clearrule:
            logging.debug(rule.tabname)
            today = datetime.date.today()
            whenday = today - datetime.timedelta(rule.reservedays)
            sqlstmt = 'select * from %s where %s <= %s and %s' \
                    %(rule.tabname, rule.datefld, whenday.strftime('%y%m%d%H%M%S'), rule.condtion) 
            query = db.GqlQuery(sqlstmt)
            for result in query:
                result.delete()
